#include "templateeditordialog.h"

TemplateEditorDialog::TemplateEditorDialog(QWidget *parent, BillTemplate* temp)
    : QDialog(parent),
      templateObj(temp ? *temp : BillTemplate())
{
    setWindowTitle("票据模板编辑器");
    resize(600, 400);
    
    initUI();
}

void TemplateEditorDialog::initUI()
{
    // 创建主布局
    QVBoxLayout* mainLayout = new QVBoxLayout(this);
    
    // 模板信息表单
    QFormLayout* formLayout = new QFormLayout();
    nameEdit = new QLineEdit(templateObj.templateName);
    widthEdit = new QLineEdit(QString::number(templateObj.templateWidth));
    heightEdit = new QLineEdit(QString::number(templateObj.templateHeight));
    
    formLayout->addRow("模板名称:", nameEdit);
    formLayout->addRow("宽度(mm):", widthEdit);
    formLayout->addRow("高度(mm):", heightEdit);
    
    mainLayout->addLayout(formLayout);
    
    // 字段列表和添加按钮
    fieldList = new QWidget();
    fieldListLayout = new QVBoxLayout(fieldList);
    
    // 加载现有字段
    for (const auto& field : templateObj.fields) {
        addFieldRow(field);
    }
    
    QPushButton* addButton = new QPushButton("添加字段");
    connect(addButton, &QPushButton::clicked, this, &TemplateEditorDialog::addNewField);
    
    mainLayout->addWidget(fieldList);
    mainLayout->addWidget(addButton);
    
    // 保存和取消按钮
    QHBoxLayout* buttonLayout = new QHBoxLayout();
    QPushButton* saveButton = new QPushButton("保存");
    QPushButton* cancelButton = new QPushButton("取消");
    
    connect(saveButton, &QPushButton::clicked, this, &TemplateEditorDialog::saveTemplate);
    connect(cancelButton, &QPushButton::clicked, this, &QDialog::reject);
    
    buttonLayout->addWidget(saveButton);
    buttonLayout->addWidget(cancelButton);
    
    mainLayout->addLayout(buttonLayout);
    
    setLayout(mainLayout);
}

void TemplateEditorDialog::addFieldRow(const FieldInfo& field)
{
    QWidget* fieldWidget = new QWidget();
    QHBoxLayout* fieldLayout = new QHBoxLayout(fieldWidget);
    
    QLineEdit* nameEdit = new QLineEdit(field.name.isEmpty() ? "字段名" : field.name);
    QLineEdit* xEdit = new QLineEdit(QString::number(field.x));
    QLineEdit* yEdit = new QLineEdit(QString::number(field.y));
    QLineEdit* widthEdit = new QLineEdit(QString::number(field.width));
    QLineEdit* heightEdit = new QLineEdit(QString::number(field.height));
    QLineEdit* fontEdit = new QLineEdit(QString::number(field.fontSize));
    QPushButton* removeButton = new QPushButton("删除");
    
    connect(removeButton, &QPushButton::clicked, [this, fieldWidget]() {
        removeField(fieldWidget);
    });
    
    fieldLayout->addWidget(new QLabel("名称:"));
    fieldLayout->addWidget(nameEdit);
    fieldLayout->addWidget(new QLabel("X:"));
    fieldLayout->addWidget(xEdit);
    fieldLayout->addWidget(new QLabel("Y:"));
    fieldLayout->addWidget(yEdit);
    fieldLayout->addWidget(new QLabel("宽度:"));
    fieldLayout->addWidget(widthEdit);
    fieldLayout->addWidget(new QLabel("高度:"));
    fieldLayout->addWidget(heightEdit);
    fieldLayout->addWidget(new QLabel("字体大小:"));
    fieldLayout->addWidget(fontEdit);
    fieldLayout->addWidget(removeButton);
    
    fieldListLayout->addWidget(fieldWidget);
}

void TemplateEditorDialog::removeField(QWidget* widget)
{
    fieldListLayout->removeWidget(widget);
    delete widget;
}

void TemplateEditorDialog::addNewField()
{
    addFieldRow();
}

BillTemplate TemplateEditorDialog::getTemplate() const
{
    return templateObj;
}

void TemplateEditorDialog::saveTemplate()
{
    // 更新模板信息
    templateObj.templateName = nameEdit->text();
    
    bool ok;
    templateObj.templateWidth = widthEdit->text().toFloat(&ok);
    if (!ok) {
        QMessageBox::warning(this, "输入错误", "宽度必须是数字");
        return;
    }
    
    templateObj.templateHeight = heightEdit->text().toFloat(&ok);
    if (!ok) {
        QMessageBox::warning(this, "输入错误", "高度必须是数字");
        return;
    }
    
    // 清空现有字段
    templateObj.fields.clear();
    
    // 收集所有字段
    for (int i = 0; i < fieldListLayout->count(); ++i) {
        QWidget* fieldWidget = fieldListLayout->itemAt(i)->widget();
        if (fieldWidget) {
            QHBoxLayout* fieldLayout = qobject_cast<QHBoxLayout*>(fieldWidget->layout());
            if (fieldLayout) {
                QString name = qobject_cast<QLineEdit*>(fieldLayout->itemAt(1)->widget())->text();
                float x = qobject_cast<QLineEdit*>(fieldLayout->itemAt(3)->widget())->text().toFloat(&ok);
                if (!ok) {
                    QMessageBox::warning(this, "输入错误", QString("第 %1 行 X 坐标必须是数字").arg(i+1));
                    return;
                }
                
                float y = qobject_cast<QLineEdit*>(fieldLayout->itemAt(5)->widget())->text().toFloat(&ok);
                if (!ok) {
                    QMessageBox::warning(this, "输入错误", QString("第 %1 行 Y 坐标必须是数字").arg(i+1));
                    return;
                }
                
                float width = qobject_cast<QLineEdit*>(fieldLayout->itemAt(7)->widget())->text().toFloat(&ok);
                if (!ok) {
                    QMessageBox::warning(this, "输入错误", QString("第 %1 行宽度必须是数字").arg(i+1));
                    return;
                }
                
                float height = qobject_cast<QLineEdit*>(fieldLayout->itemAt(9)->widget())->text().toFloat(&ok);
                if (!ok) {
                    QMessageBox::warning(this, "输入错误", QString("第 %1 行高度必须是数字").arg(i+1));
                    return;
                }
                
                float fontSize = qobject_cast<QLineEdit*>(fieldLayout->itemAt(11)->widget())->text().toFloat(&ok);
                if (!ok) {
                    QMessageBox::warning(this, "输入错误", QString("第 %1 行字体大小必须是数字").arg(i+1));
                    return;
                }
                
                FieldInfo field;
                field.name = name;
                field.x = x;
                field.y = y;
                field.width = width;
                field.height = height;
                field.fontSize = fontSize;
                
                templateObj.fields.append(field);
            }
        }
    }
    
    accept();
}
